package com.ruoyi.ldzlsc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ldzlsc.domain.ScProductionOrder;
import com.ruoyi.ldzlsc.service.ScProductionOrderService;

/**
 * 生产订单Controller
 *
 * @author ldzl
 * @date 2023-09-15
 */
@RestController
@RequestMapping("/sc/productionOrder")
public class ScProductionOrderController extends BaseController
{
    @Autowired
    private ScProductionOrderService scProductionOrderService;

    /**
     * 查询生产订单列表
     */
    //@RequiresPermissions("sc:productionOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(ScProductionOrder scProductionOrder)
    {
        startPage();
        List<ScProductionOrder> list = scProductionOrderService.selectScProductionOrderList(scProductionOrder);
        return getDataTable(list);
    }

    /**
     * 获取生产订单详细信息
     */
    //@RequiresPermissions("sc:productionOrder:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(scProductionOrderService.selectScProductionOrderWithAllDetails(id));
    }
    
    /**
     * 修改生产订单
     */
    //@RequiresPermissions("sc:productionOrder:edit")
    @PutMapping
    @Log(title = "生产订单", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody ScProductionOrder scProductionOrder)
    {
        return toAjax(scProductionOrderService.updateScProductionOrder(scProductionOrder));
    }
    
    /**
     * 新增生产订单
     */
    //@RequiresPermissions("sc:productionOrder:add")
    @PostMapping
    @Log(title = "生产订单", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody ScProductionOrder scProductionOrder)
    {
        // 添加调试日志
        System.out.println("=== 生产订单新增调试信息 ===");
        System.out.println("订单编号: " + scProductionOrder.getProdOrderCode());
        System.out.println("自动生成计划标记: " + scProductionOrder.getAutoGeneratePlan());
        System.out.println("状态: " + scProductionOrder.getStatus());
        System.out.println("产品明细数量: " + (scProductionOrder.getProducts() != null ? scProductionOrder.getProducts().size() : 0));

        return toAjax(scProductionOrderService.insertScProductionOrder(scProductionOrder));
    }
    
    /**
     * 删除生产订单
     */
    //@RequiresPermissions("sc:productionOrder:remove")
    @DeleteMapping("/{ids}")
    @Log(title = "生产订单", businessType = BusinessType.DELETE)
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(scProductionOrderService.deleteScProductionOrderByIds(ids));
    }
}